package group2.core.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;

/**
 * This class contains all the data on the posts.
 * 
 * @author mattias
 */
@Entity
public class Post implements Serializable {

    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    private final static int PREVIEW_SIZE = 100;
    
    @Temporal(javax.persistence.TemporalType.DATE)
    @Column(name = "POSTDATE")
    private Date date;
    
    @Column(nullable = false, length = 50)
    private String title;
    
    @Column(nullable = false, length = 8192)
    private String content;

    @ManyToOne()
    @JoinColumn(name = "AUTHOR_FK",nullable=false)
    private CamelUser postAuthor;
    
    @OneToMany(mappedBy = "post", cascade = {CascadeType.REMOVE, CascadeType.PERSIST})
    private List<Comment> comments;

    public Post() {
    }

    public Post(long id, String title, String content,CamelUser postAuthor) {
        this.id = id;
        this.date = Calendar.getInstance().getTime();
        this.title = title;
        this.content = content;
        this.comments = new ArrayList();
        this.postAuthor = postAuthor;
    }

     public Date getDate() {
        return new Date(date.getTime());
    }

    public void setDate(Date date) {
        this.date = new Date(date.getTime());
    }

    public CamelUser getUserClass() {
        return postAuthor;
    }

    public void setUserClass(CamelUser userClass) {
        this.postAuthor = userClass;
    }

    public List<Comment> getComments() {
        return comments;
    }
    
    public void addComment(Comment c){
        comments.add(c);
        c.setPost(this);
    }
    
    public void removeComment (Comment c){
        comments.remove(c);
    }
    
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }
    
    /**
     * Returns a preview of the post that is PREVIEW_SIZE characters long
     * @return The beginning of the post, for previewing
     */
      public String getPostPreview(){
        if(this.getContent().length() < PREVIEW_SIZE)
            return this.getContent();
        else 
            return this.getContent().substring(0, PREVIEW_SIZE) + "...";
                    
            
    }
    /**
     * 
     * @param object
     * @return false if objects differ or both are null.
     */
    @Override
    public boolean equals(Object object) {
    //This method will return false is this==null && other==null
        if (object==null || !(object instanceof Post)) {
            return false;
        }
        Post other = (Post) object;
        
        if(this.id==null && other.id==null){
            return false;
        }
        
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "group2.core.Post[ id=" + id + " ]";
    }
}
